<template>
  <div class="common-layout rabbit-theme">
    <el-container>
      <el-header class="lavender-header">
        <div class="header-content">
          <div class="header-title">
            <div class="rabbit-logo">🐰</div>
            <span>溯源管理系统</span>
          </div>
          <div class="user-info">
            欢迎您，{{ userName }}
            <el-button type="primary" class="logout-btn" @click="handleLogout"
              >退出</el-button
            >
          </div>
        </div>
        <!-- 兔子装饰元素 - 耳朵 -->
        <div class="rabbit-ear left-ear"></div>
        <div class="rabbit-ear right-ear"></div>
      </el-header>
      <el-container>
        <el-aside width="220px" class="lavender-aside">
          <div class="bunny-decoration top"></div>
          <div class="bunny-decoration bottom"></div>
          <el-menu
            active-text-color="#663399"
            background-color="#f0e6ff"
            class="lavender-menu"
            :default-active="$route.path"
            text-color="#5d4777"
            router
          >
            <el-sub-menu
              v-for="(a, index) in url"
              :key="index"
              :index="a.url || index.toString()"
            >
              <template #title>
                <div class="rabbit-menu-icon">🐇</div>
                <span>{{ a.menuName }}</span>
              </template>

              <el-menu-item
                v-for="(b, bIndex) in a.children"
                :key="bIndex"
                :index="b.url || `${index}-${bIndex}`"
                class="carrot-item"
              >
                <div class="carrot-icon">🥕</div>
                <span>{{ b.menuName }}</span>
              </el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main class="content-main">
          <div class="scrollable-content">
            <div class="rabbit-paw top-left"></div>
            <div class="rabbit-paw top-right"></div>
            <div class="rabbit-paw bottom-left"></div>
            <div class="rabbit-paw bottom-right"></div>
            <RouterView />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { RouterView, useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const route = useRoute();
const router = useRouter();
import axios from "axios";
import { onMounted } from "vue";
import { ref } from "vue";

import httpr from "@/axioslan/axioshttp";
onMounted(() => {
  CascdeMenuget();
});

const userName = ref(localStorage.getItem("userName") || "用户");

const url = ref([
  {
    menuId: "",
    menuName: "",
    url: "",
    children: [
      {
        menuId: 4,
        menuName: "",
        url: "",
        children: [],
      },
    ],
  },
]);

const CascdeMenuget = () => {
  let roleid = localStorage.getItem("roleid");
  httpr({
    url: "/api/Menu/GetMenuByRoleId",
    method: "get",
    params: {
      roleId: roleid,
    },
  }).then((res) => {
    url.value = res.data.data;
  });
};

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm("确定要退出系统吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      router.push("/");
      ElMessage.success("已退出系统");
    })
    .catch(() => {});
};
</script>

<style scoped>
.common-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f9f5ff;
}

.rabbit-theme {
  position: relative;
  overflow: hidden;
}

.lavender-header {
  background: linear-gradient(135deg, #d8c6f7 0%, #b292ea 100%);
  color: #5d4777;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  height: 60px;
  position: relative;
  overflow: hidden;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
  position: relative;
  z-index: 2;
}

.header-title {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
  gap: 10px;
}

.rabbit-logo {
  font-size: 24px;
  animation: hop 2s infinite;
}

@keyframes hop {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

/* 兔子耳朵装饰 */
.rabbit-ear {
  position: absolute;
  width: 30px;
  height: 80px;
  background-color: #e6d4ff;
  border-radius: 50% 50% 0 0;
  top: -50px;
  z-index: 1;
  transform-origin: bottom center;
}

.left-ear {
  left: 20%;
  transform: rotate(-10deg);
}

.right-ear {
  right: 20%;
  transform: rotate(10deg);
}

.rabbit-ear:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 60px;
  background-color: #ffd6ec;
  border-radius: 50% 50% 0 0;
  top: 5px;
  left: 5px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logout-btn {
  background-color: #a57fe5;
  border-color: #a57fe5;
}

.logout-btn:hover {
  background-color: #8a5bd6;
  border-color: #8a5bd6;
}

.lavender-aside {
  background-color: #f0e6ff;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  position: relative;
}

.bunny-decoration {
  position: absolute;
  width: 40px;
  height: 40px;
  background-image: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="%23a57fe5" d="M330.7 352l77.8-192H473l-77.8 192h-64.5zm-37.7 0l77.8-192h-62.9l-77.8 192h62.9zm-74.1 0l77.8-192h-62.9l-77.8 192h62.9zm-76.1 0l77.8-192H83.8l-77.8 192h136.8z"/><circle fill="%23a57fe5" cx="256" cy="256" r="256"/><path fill="%23f0e6ff" d="M368 96c0 26.5-21.5 48-48 48s-48-21.5-48-48 21.5-48 48-48 48 21.5 48 48zm-48 336c-88.4 0-160-71.6-160-160h-48c0 114.9 93.1 208 208 208s208-93.1 208-208h-48c0 88.4-71.6 160-160 160zm-48-160c0-26.5 21.5-48 48-48s48 21.5 48 48-21.5 48-48 48-48-21.5-48-48z"/></svg>');
  background-repeat: no-repeat;
  background-size: contain;
  opacity: 0.2;
}

.bunny-decoration.top {
  top: 20px;
  right: 20px;
  animation: float 4s ease-in-out infinite;
}

.bunny-decoration.bottom {
  bottom: 20px;
  left: 20px;
  animation: float 5s ease-in-out infinite reverse;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0) rotate(0);
  }
  50% {
    transform: translateY(-10px) rotate(5deg);
  }
}

.lavender-menu {
  border-right: none;
  margin-top: 15px;
}

.rabbit-menu-icon,
.carrot-icon {
  margin-right: 8px;
  font-size: 18px;
  display: inline-block;
}

.lavender-menu :deep(.el-sub-menu__title) {
  color: #5d4777 !important;
  position: relative;
}

.lavender-menu :deep(.el-sub-menu__title:hover) {
  background-color: #e3d7f4 !important;
}

.lavender-menu :deep(.el-menu-item.is-active) {
  background-color: #d8c6f7 !important;
  color: #663399 !important;
}

.lavender-menu :deep(.el-menu-item:hover) {
  background-color: #e3d7f4 !important;
}

.carrot-item:hover .carrot-icon {
  animation: wiggle 0.5s ease-in-out;
}

@keyframes wiggle {
  0%,
  100% {
    transform: rotate(0);
  }
  25% {
    transform: rotate(15deg);
  }
  50% {
    transform: rotate(0);
  }
  75% {
    transform: rotate(-15deg);
  }
}

.content-main {
  background-color: #faf7ff;
  padding: 20px;
  position: relative;
  height: calc(100vh - 60px);
  overflow: auto;
}

.scrollable-content {
  position: relative;
  min-height: 100%;
}

/* 兔子脚印装饰 */
.rabbit-paw {
  position: absolute;
  width: 30px;
  height: 20px;
  background-image: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="%23e6d4ff" d="M256 0C114.6 0 0 114.6 0 256s114.6 256 256 256s256-114.6 256-256S397.4 0 256 0zM64 224c0-17.7 14.3-32 32-32s32 14.3 32 32s-14.3 32-32 32s-32-14.3-32-32zm96 96c-17.7 0-32-14.3-32-32s14.3-32 32-32s32 14.3 32 32s-14.3 32-32 32zm32-160c0 17.7-14.3 32-32 32s-32-14.3-32-32s14.3-32 32-32s32 14.3 32 32zM256 384c-17.7 0-32-14.3-32-32s14.3-32 32-32s32 14.3 32 32s-14.3 32-32 32zm0-192c-35.3 0-64-28.7-64-64s28.7-64 64-64s64 28.7 64 64s-28.7 64-64 64zm32 128c0-17.7 14.3-32 32-32s32 14.3 32 32s-14.3 32-32 32s-32-14.3-32-32zm128-64c-17.7 0-32-14.3-32-32s14.3-32 32-32s32 14.3 32 32s-14.3 32-32 32z"/></svg>');
  background-repeat: no-repeat;
  background-size: contain;
  opacity: 0.2;
  transform: rotate(45deg);
  z-index: 0;
}

.rabbit-paw.top-left {
  top: 40px;
  left: 60px;
}

.rabbit-paw.top-right {
  top: 80px;
  right: 100px;
  transform: rotate(135deg);
}

.rabbit-paw.bottom-left {
  bottom: 50px;
  left: 120px;
  transform: rotate(-45deg);
}

.rabbit-paw.bottom-right {
  bottom: 70px;
  right: 70px;
  transform: rotate(225deg);
}

.el-container {
  height: 100%;
}
</style>
